package com.zhao.dota.security.token;

import cn.hutool.core.date.SystemClock;
import cn.hutool.core.lang.UUID;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map;
import java.util.Set;


@Data
public class InternalAccessToken implements AccessToken, Serializable {

    private static int defaultTimeout = 60 * 60;

    private Set<String> scope;

    private Map<String, Object> additionalInformation;

    private String refreshToken;

    private String tokenType;

    private LocalDateTime expiration;

    private int expiresIn;

    private String value;

    private int timeout;


    @Override
    public boolean isExpired() {
        return SystemClock.now() / 1000 > expiresIn;
    }

    public InternalAccessToken() {
        this((int) (SystemClock.now() / 1000) + defaultTimeout);
    }

    public InternalAccessToken(int expiresIn) {
        this(LocalDateTime.ofEpochSecond(expiresIn, 0, ZoneOffset.of("+8")));
    }

    public InternalAccessToken(LocalDateTime expiration) {
        this.expiration = expiration;
        this.expiresIn = (int) expiration.toEpochSecond(ZoneOffset.of("+8"));
        this.value = UUID.randomUUID().toString(true);
        this.refreshToken = UUID.randomUUID().toString(true);
        this.timeout = this.expiresIn - (int) (SystemClock.now() / 1000);
    }

}
